package com.retrocc.coco.controller;

import com.retrocc.coco.mapper.UserMapper;
import com.retrocc.coco.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
public class LoginController {

	@Autowired
	UserMapper userMapper;

	@Autowired
	UserService userService;

@PostMapping(value = "/user/login")
public String login(@RequestParam("username") String username,
                    @RequestParam("password") String password,
                    Map<String, Object> map, HttpSession session) {
//	if ("admin".equals(username) && "123456".equals(password)) {
//	AdminUser user = userDao.findAdminUserByUsernameAndPassword(username, password);
	boolean exits = userService.userExits(username, password);
	if(exits){
	//登陆成功，防止表单重复提交，重定向到主页
		session.setAttribute("loginUser", username);
		System.out.println("登录成功");
//		return "success";
		return "redirect:/success.html";
	} else {
		//登陆失败

		map.put("msg", "用户名或者密码错误");
		return "login";
	}

}
}
